import pgzrun
import random


flag = 0  # 游戏环节 0表示开始阶段，1表示第一次跳起阶段，2表示游戏阶段，3表示结束阶段
Jump = 0  # 是否跳跃 flag为2时
i = 0  # flag为1时跳跃时间
j = 0  # flag为2时跳跃时间
t = 0  # 用于出现仙人掌的时间
k = 0  # 用于计分的时间
WIDTH = 800
HEIGHT = 380
score = 0  # 积分
f = open('score', 'r')
score_max = int(f.read())  # 文件中第一次游戏时为0
f.close()


bg1 = Actor('background')
bg2 = Actor('background')
bg2.left = 800
dino = Actor('dino4')  # dino4为静止或跳起
dino.x = 50
dino.y = 310
plants = []  # 用于存储仙人掌角色
game_over = Actor('game over')
game_over.x = 400
game_over.y = 130
ret = Actor('return')
ret.x = 400
ret.y = 160


def back():
    global flag, Jump, plants, j, i, t, k, score
    Jump = 0
    flag = 2
    dino.image = 'dino1'
    dino.y = 310
    bg1.left = 0
    bg2.left = 800
    plants = []
    j = 0
    i = 0
    t = 0
    k = 0
    score = 0


def draw():
    global flag
    screen.clear()
    bg1.draw()
    bg2.draw()
    dino.draw()
    score_str = str(score)
    score_max_str = str(score_max)
    if len(score_str) < 5:
        score_str = '0' * (5 - len(score_str)) + score_str
    elif len(score_str) > 5:
        score_str = '99999'
    if len(score_max_str) < 5:
        score_max_str = '0' * (5 - len(score_max_str)) + score_max_str
    elif len(score_max_str) > 5:
        score_max_str = '99999'
    screen.draw.text('HI   ' + score_max_str + '  ' + score_str, (600, 10), color='black')
    for p in plants:
        p.draw()
    if flag == 3:
        game_over.draw()
        ret.draw()


def on_key_down(key):
    global flag, Jump
    if flag == 0:
        if key == keys.SPACE:
            flag = 1
    elif flag == 2:
        if key == keys.SPACE or key == keys.UP:
            Jump = 1
    elif flag == 3:
        if key == keys.SPACE:
            back()


def on_mouse_down(pos, button):
    global flag
    if flag == 3:
        if button == mouse.LEFT and ret.collidepoint(pos):
            back()


def update():
    global flag, i, Jump, j, t, k, score, score_max
    if flag == 1:
        if i <= 15:
            dino.y -= 10
            i += 1
        elif i <= 31:
            dino.y += 10
            i += 1
        else:
            i = 0
            flag = 2
            dino.image = 'dino1'
    if flag == 2:
        # 两块背景交替出现，小恐龙图片重复切换，形成小恐龙跑动的效果
        bg1.x -= 10
        bg2.x -= 10
        if bg1.right <= 0:
            bg1.left = 800
        if bg2.right <= 0:
            bg2.left = 800
        if dino.image == 'dino1':  # dino1为左脚落地
            i += 1
            if i == 5:
                dino.image = 'dino2'
                i = 0
        elif dino.image == 'dino2':  # dino2为右脚落地
            i += 1
            if i == 5:
                dino.image = 'dino1'
                i = 0
        for p in plants:
            p.x -= 10
            if p.colliderect(dino):
                flag = 3
                dino.image = 'dino3'  # dino3为死亡形态
        if Jump:
            dino.image = 'dino4'
            if j <= 15:
                dino.y -= 10
                j += 1
            elif j <= 31:
                dino.y += 10
                j += 1
            else:
                j = 0
                Jump = 0
                dino.image = 'dino1'
        t += 1
        if t == 90:  # update函数每秒执行60次，1.5秒产生一个新的仙人掌
            a = random.randint(1, 4)  # 1到4的随机数
            p = Actor('plant' + str(a))  # 仙人掌图片命名为plant1到plant4
            p.x = 800
            p.y = 300
            plants.append(p)
            t = 0
        k += 1
        if k == 6:  # update函数每秒执行60次，每0.1秒加一分
            score += 1
            k = 0
            if score >= score_max:
                score_max = score
                fi = open('score', 'w')
                fi.write(str(score_max))
                fi.close()


pgzrun.go()
